package cn.btks.backstagemanage.menu.repository;

import cn.btks.commonserver.util.sm2.SM2EncDecUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author lijun
 * @Date 2021/1/18 12:34
 */
@Repository
@Transactional
public class MenuBaseRepository {

    @Autowired
    MenuRepository menuRepository;

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 更新资源清单及角色关联表（先删除关联数据，再新增角色下的资源清单数据）
     * @param roleId
     * @param menuIdList
     */
    public void updateMenuRoles(Long roleId, List<Long> menuIdList){
        int count = menuRepository.isExistMenuRoles(roleId);
        if(count > 0){
            int result = menuRepository.deleteMenuRolesByRolesId(roleId);
        }
        String sql = "insert into menu_roles(menu_id,roles_id) values (?,?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Long menuId = menuIdList.get(i);
                preparedStatement.setLong(1, menuId);
                preparedStatement.setLong(2, roleId);
            }

            @Override
            public int getBatchSize() {
                return menuIdList.size();
            }
        });
    }

    /**
     * 更新资源清单及角色关联表（先删除关联数据，再新增角色下的资源清单数据）（加密）
     * @param roleId
     * @param menuIdList
     */
    public void updateMenuRoles1(Long roleId, List<Long> menuIdList) throws IOException {
        int count = menuRepository.isExistMenuRoles1(SM2EncDecUtils.encrypt(String.valueOf(roleId)));
        if(count > 0){
            int result = menuRepository.deleteMenuRolesByRolesId1(SM2EncDecUtils.encrypt(String.valueOf(roleId)));
        }
        String sql = "insert into menu_roles(menu_id,roles_id) values (?,?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @SneakyThrows
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Long menuId = menuIdList.get(i);
                preparedStatement.setString(1, SM2EncDecUtils.encrypt(String.valueOf(menuId)));
                preparedStatement.setString(2, SM2EncDecUtils.encrypt(String.valueOf(roleId)));
            }

            @Override
            public int getBatchSize() {
                return menuIdList.size();
            }
        });
    }

    /**
     * 新增资源清单及角色关联表（先删除关联数据，再新增角色下的资源清单数据）（加密）
     * @param roleId
     * @param menuIdList
     */
    public void saveMenuRoles(Long roleId, List<Long> menuIdList) throws IOException {
        String sql = "insert into menu_roles(menu_id,roles_id) values (?,?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @SneakyThrows
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Long menuId = menuIdList.get(i);
                preparedStatement.setString(1, SM2EncDecUtils.encrypt(String.valueOf(menuId)));
                preparedStatement.setString(2, SM2EncDecUtils.encrypt(String.valueOf(roleId)));
            }

            @Override
            public int getBatchSize() {
                return menuIdList.size();
            }
        });
    }
}
